package com.gome.ocean.dao.model.gddl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import com.gome.ocean.common.enums.ShardAlgorithm;
import com.gome.ocean.common.enums.ShardOrder;

/**
 * 创建分库分表
 * 
 * @author haqiaolong 2016年4月7日 上午11:08:04
 * @since 5.0.0
 */
public class GddlShardAppVO {

    /**
     * 物理表前缀
     */
    private String physicsTbPrefix;

    /**
     * 应用名称
     */
    private String appName;
    /**
     * 逻辑表名
     */
    private String logicTbName;

    /**
     * 分表字段
     */
    private String  shardTbFild;
    /**
     * 分表数量
     */
    private Integer shardTbCount;

    /**
     * 数据源集合
     */
    private List<Long> shardDbs;

    /**
     * 是否有备库
     */
    private boolean isBack;

    /**
     * 备库集合
     */
    private List<Long> backDbs;

    /**
     * 分库数量
     */
    private Integer shardDbCount;

    /**
     * 后缀长度
     */
    private Integer suffixLength;

    /**
     * 分库表名是否相同
     */
    private boolean shardDbTbSameName;

    /**
     * 分表顺序
     */
    private ShardOrder shardOrder;

    /**
     * 分表顺序数量
     */
    private int            shardNum;
    /**
     * 分库分表算法
     */
    private ShardAlgorithm shardAlgorithm;

    /**
     * 是否新建表
     */
    private boolean isCreateTable;

    /**
     * 执行SQL 采用{0}占位符
     */
    private String shardSql;

    /**
     * 版本号
     */
    private String version;

    /**
     * 工程组名
     */
    private String projectName;

    /**
     * 排序分组
     */
    private Map<String, Set<String>> actualTopology;

    /**
     * 新的数据结构
     */
    private List<GddlShardAppVO> gddlAttrMap;

    public List<GddlShardAppVO> getGddlAttrMap() {
        return gddlAttrMap;
    }

    public void setGddlAttrMap(List<GddlShardAppVO> gddlAttrMap) {
        this.gddlAttrMap = gddlAttrMap;
    }

    public boolean isCreateTable() {
        return isCreateTable;
    }

    public void setCreateTable(boolean isCreateTable) {
        this.isCreateTable = isCreateTable;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getPhysicsTbPrefix() {
        return physicsTbPrefix;
    }

    public void setPhysicsTbPrefix(String physicsTbPrefix) {
        this.physicsTbPrefix = physicsTbPrefix;
    }

    public String getLogicTbName() {
        return logicTbName;
    }

    public void setLogicTbName(String logicTbName) {
        this.logicTbName = logicTbName;
    }

    public String getShardTbFild() {
        return shardTbFild;
    }

    public void setShardTbFild(String shardTbFild) {
        this.shardTbFild = shardTbFild;
    }

    public Integer getShardTbCount() {
        return shardTbCount;
    }

    public void setShardTbCount(Integer shardTbCount) {
        this.shardTbCount = shardTbCount;
    }

    public List<Long> getShardDbs() {
        return shardDbs;
    }

    public void setShardDbs(List<Long> shardDbs) {
        this.shardDbs = shardDbs;
    }

    public String getSuffixLength() {
        StringBuffer suffix = new StringBuffer(suffixLength);
        for (int i = 0; i < suffixLength; i++) {
            suffix.append("0");
        }
        return suffix.toString();
    }

    public void setSuffixLength(Integer suffixLength) {
        this.suffixLength = suffixLength;
    }

    public boolean isShardDbTbSameName() {
        return shardDbTbSameName;
    }

    public void setShardDbTbSameName(boolean shardDbTbSameName) {
        this.shardDbTbSameName = shardDbTbSameName;
    }

    public ShardAlgorithm getShardAlgorithm() {
        return shardAlgorithm;
    }

    public void setShardAlgorithm(ShardAlgorithm shardAlgorithm) {
        this.shardAlgorithm = shardAlgorithm;
    }

    public Integer getShardDbCount() {
        return shardDbCount;
    }

    public void setShardDbCount(Integer shardDbCount) {
        this.shardDbCount = shardDbCount;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getShardSql() {
        return shardSql;
    }

    public void setShardSql(String shardSql) {
        this.shardSql = shardSql;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public List<Long> getBackDbs() {
        return backDbs;
    }

    public void setBackDbs(List<Long> backDbs) {
        this.backDbs = backDbs;
    }

    public boolean isBack() {
        return isBack;
    }

    public void setBack(boolean isBack) {
        this.isBack = isBack;
    }

    public ShardOrder getShardOrder() {
        return shardOrder;
    }

    public void setShardOrder(ShardOrder shardOrder) {
        this.shardOrder = shardOrder;
    }

    public int getShardNum() {
        return shardNum;
    }

    public void setShardNum(int shardNum) {
        this.shardNum = shardNum;
    }

    public TreeMap<String, Set<String>> getActualTopology() {
        TreeMap<String, Set<String>> treeMap = new TreeMap<String, Set<String>>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                int onum = Integer.parseInt(StringUtils.substringAfterLast(o1, "_"));
                int nnum = Integer.parseInt(StringUtils.substringAfterLast(o2, "_"));
                if (onum < nnum) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        if (null != this.actualTopology) {
            for (Entry<String, Set<String>> topology : this.actualTopology.entrySet()) {
                treeMap.put(topology.getKey(), topology.getValue());
            }
        }
        return treeMap;
    }

    public void setActualTopology(Map<String, Set<String>> actualTopology) {
        this.actualTopology = actualTopology;
    }
}
